from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from twisted.conch.ssh.connection import value

from user.models import Dynamic, Video, Essay
from user.ser import DynamicSer, VideoSer, EssaySer


# Create your views here.
# 推荐接口
class tuiRecentPublishView(APIView):
    def get(self, request):
        # 最近动态
        dynamic = Dynamic.objects.filter(num__gt=2000)
        dynamic_ser = DynamicSer(dynamic, many=True)
        # 最近动态
        video = Video.objects.filter(num__gt=2000)
        video_ser = VideoSer(video, many=True)
        # 最近文章
        essay = Essay.objects.filter(num__gt=2000)
        essay_ser = EssaySer(essay, many=True)
        data = []
        for i in dynamic_ser.data:
            i["imglist"] = i["imglist"].split(',')
        for i in dynamic_ser.data:
            i['kind'] = 2
            data.append(i)
        for i in video_ser.data:
            i['kind'] = 1
            data.append(i)
        for i in essay_ser.data:
            i['kind'] = 0
            data.append(i)
        data.sort(key=lambda x: x['addtime'], reverse=True)
        return Response({"code": 200, 'data': data})

# 热榜展示
class HotRecentPublishView(APIView):
    def get(self,request):
        essay=Essay.objects.filter().order_by("-num")
        essay_ser=EssaySer(essay,many=True)
        return Response({"code":200,"data":essay_ser.data})